<?php
/**
 * Created by PhpStorm.
 * User: 王溪
 * Date: 2020/7/2
 * Time: 20:06
 */

namespace app\index\controller;


class Menu extends Base
{

    /**
     *  权限
     */
    public function permission()
    {

        $a = array(
            '1' => array(
                'is_get' => 1,
                'is_do' => 0,
                'is_excute' => 1,
            ),

            '2' => array(
                'is_get' => 1,
                'is_do' => 1,
                'is_excute' => 1,
            ),
        );

        foreach ($a as $per_id => $update){

        }

        foreach ($a as $permission_id => $v){



        }





        $userId = $userGroupId = 1;

        $field = 'sp.id, sp.parent_id, is_parent, text, path, type, name, is_get, is_do, is_excute';

        $menu = db('ServerUserPermissionsRelation')->alias('supr')
            ->join('server_permissions sp', 'supr.permissions_id = sp.id')
            ->where('user_id', $userId)
            ->where('user_group_id', $userGroupId)
            ->order('sort','asc')
            ->field($field)
            ->select();
//        dump($menu);die;
//        dump($menu);die;


        $menu_list = array();

        $this->permissionD($menu, $menu_list,0,0);

       // dump($menu_list);die;

        echo json_encode($menu_list);die;
    }

    function permissionD($data, &$list_array, $array_id, $parent_id)
    {
        foreach ($data as $key => $value) {
            if ($value['parent_id'] == $parent_id) {

                $array_tmp  = array(
                    "text"=>$value['text'],
                    "path"=>$value['path'],
                    "type"=>$value['type'],
                    "name"=>$value['name'],
                    "is_get"=>$value['is_get'],
                    "is_do"=>$value['is_do'],
                    "is_excute"=>$value['is_excute'],
                    "children"=>array()
                );



                $list_array[$array_id]=$array_tmp;

                if($value['is_parent'] == 1)
                {
                    $this->permissionD($data, $list_array[$array_id]['children'], 0, $value['id']);
                }

                $array_id++;
            }
        }

    }

    /**
     *  菜单
     */
    public function index()
    {

        $userId = $userGroupId = 1;

        $field = 'sp.id, sp.parent_id, is_parent, text, path, type, name, is_get, is_do, is_excute';

        $menu = db('ServerUserPermissionsRelation')->alias('supr')
            ->join('server_permissions sp', 'supr.permissions_id = sp.id')
            ->where('user_id', $userId)
            ->where('user_group_id', $userGroupId)
            ->where('hidden', 0)
            ->order('sort','asc')
            ->field($field)
            ->select();

        $menu_list = array();

        $this->createMenu($menu, $menu_list,0,0);

        echo json_encode($menu_list);die;

        dump($menu_list);
    }

    /**
     * @name 生成菜单
     * @param $data
     * @param $list_array
     * @param $array_id
     * @param $parent_id
     * @author wx
     * @date 2020/6/24 9:33
     */
    function createMenu($data, &$list_array, $array_id, $parent_id)
    {
        foreach ($data as $key => $value) {
            if ($value['parent_id'] == $parent_id) {

                if($value['is_get'] == 0){//无查看权限,就隐藏该菜单
                    continue;
                }

                $array_tmp  = array(
                    "text"=>$value['text'],
                    "path"=>$value['path'],
                    "type"=>$value['type'],
                    "name"=>$value['name'],
                    "is_get"=>$value['is_get'],
                    "is_do"=>$value['is_do'],
                    "is_excute"=>$value['is_excute'],
                    "children"=>array()
                );



                $list_array[$array_id]=$array_tmp;

                if($value['is_parent'] == 1)
                {
                    $this->createmenu($data, $list_array[$array_id]['children'], 0,$value['id']);
                }

                $array_id++;
            }
        }

    }




}